
package com.bluelinelabs.conductor.demo.controllers;

import com.bluelinelabs.conductor.conductor.manager.ObjectManager;
import ohos.aafwk.content.IntentParams;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;

import com.bluelinelabs.conductor.conductor.RouterTransaction;
import com.bluelinelabs.conductor.conductor.changehandler.FadeChangeHandler;
import com.bluelinelabs.conductor.demo.ResourceTable;
import com.bluelinelabs.conductor.demo.controllers.base.BaseController;

/**
 * lipeiquan
 *
 * @since 2021-05-06
 */
public class ExternalModulesController extends BaseController {
    public ExternalModulesController() {
        super(ResourceTable.Layout_controller_additional_modules);
    }

    @Override
    public void onViewCreated(Component component) {
        title = "External Module Demos";
        ObjectManager.getInstance().setIdTitleAboutSetVisibility(Component.HIDE);
        ListContainer Id_recycler_view = (ListContainer) component.findComponentById(ResourceTable.Id_recycler_view);

        component.getContext().getUITaskDispatcher().delayDispatch(new Runnable() {
            @Override
            public void run() {
                Id_recycler_view.setItemProvider(new AdditionalModulesAdapter());
            }
        },600);
    }

    private void onModelRowClick(ModuleModel model) {
        switch (model) {
            case AUTODISPOSE:
                title = "Autodispose Demo";
                AutodisposeController autodisposeController = new AutodisposeController();
                autodisposeController.changeTAG("AutodisposeController");
                getRouter().pushController(
                    RouterTransaction.with(autodisposeController)
                        .pushChangeHandler(new FadeChangeHandler())
                        .popChangeHandler(new FadeChangeHandler())
                );
                break;
            case RX_LIFECYCLE_2:
                title = "RxLifecycle2 Demo";
                AutodisposeController autodisposeController2 = new AutodisposeController();
                autodisposeController2.changeTAG("RxLifecycleController");
                getRouter().pushController(
                    RouterTransaction.with(autodisposeController2)
                        .pushChangeHandler(new FadeChangeHandler())
                        .popChangeHandler(new FadeChangeHandler())
                );
                break;
            case ARCH_LIFECYCLE:
                title = "Arch Components Lifecycle Demo";
                IntentParams intentParams = new IntentParams();
                intentParams.setParam(AutodisposeController.KEY_BackgroundColor, getActivity().getString(ResourceTable.Color_orange_300));
                intentParams.setParam(TextController.KEY_NEXT_RELEASE,"Logcat should now report that the Controller's onDetach() and LifecycleObserver's onPause() methods were called, followed by the Controller's onDestroyView() and LifecycleObserver's onStop().");
                intentParams.setParam(TextController.KEY_NEXT_RETAIN,"Logcat should now report that the Controller's onDetach() and LifecycleObserver's onPause() methods were called.");
                AutodisposeController autodisposeController1 = new AutodisposeController(intentParams);
                autodisposeController1.changeTAG("ArchLifecycleController");
                getRouter().pushController(
                    RouterTransaction.with(autodisposeController1)
                        .pushChangeHandler(new FadeChangeHandler())
                        .popChangeHandler(new FadeChangeHandler())
                );
                break;
        }
        changeToolbarContent();
    }

    private enum ModuleModel {
        AUTODISPOSE("Autodispose", ResourceTable.Color_purple_300),
        RX_LIFECYCLE_2("Rx Lifecycle 2", ResourceTable.Color_blue_grey_300),
        ARCH_LIFECYCLE("Arch Components Lifecycle", ResourceTable.Color_orange_300);
        String title;
        int color;

        ModuleModel(String title, int color) {
            this.title = title;
            this.color = color;
        }

    }


    private class AdditionalModulesAdapter extends BaseItemProvider {


        @Override
        public int getCount() {
            return ModuleModel.values().length;
        }

        @Override
        public Object getItem(int i) {
            return i;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {

            Component cpt;
            if (convertComponent == null) {
                cpt = LayoutScatter.getInstance(componentContainer.getContext()).parse(ResourceTable.Layout_row_home, componentContainer, false);
            } else {
                cpt = convertComponent;
            }

            Text Id_title = (Text) cpt.findComponentById(ResourceTable.Id_title);
            Id_title.setText(ModuleModel.values()[position].title);

            //按钮
            ShapeElement shapeElement = new ShapeElement(cpt.getContext(), ResourceTable.Graphic_circle);
            shapeElement.setRgbColor(RgbColor.fromArgbInt(componentContainer.getContext().getColor(ModuleModel.values()[position].color)));
            ((Image) cpt.findComponentById(ResourceTable.Id_dot)).setBackground(shapeElement);
            cpt.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    onModelRowClick(ModuleModel.values()[position]);
                }
            });

            return cpt;
        }
    }


}
